package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.*;
import com.jy.wms.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: WmsReceivingService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Tue Sep 26 17:30:08 CST 2017
 */

@Service
public class WmsReceivingService extends BaseService<WmsReceivingMapper, WmsReceiving> {
    @Autowired
    private WmsPalletSortingMapper wmsPalletSortingMapper;
    @Autowired
    private WmsEstimatedArrivalOrderMapper wmsEstimatedArrivalOrderMapper;
    @Autowired
    private WmsEstimatedArrivalOrderDetailedMapper wmsEstimatedArrivalOrderDetailedMapper;
    @Autowired
    private WmsStockMapper wmsStockMapper;
    @Autowired
    private WmsPurchaseOrderDetailedMapper wmsPurchaseOrderDetailedMapper;
    @Autowired
    private WmsPurchaseOrderMapper wmsPurchaseOrderMapper;
    @Autowired
    private WmsGoodsMapper wmsGoodsMapper;
    @Autowired
    private WmsGoodsLocationMapper wmsGoodsLocationMapper;
    @Autowired
    private WmsShelvesMapper wmsShelvesMapper;

    /**
     * 收货确认
     *
     * @param sortings 收货信息参数
     * @return int
     */

    @BaseServiceDescribe(moduleName = "确认收货", btnName = "确认收货按钮")
    @Transactional
    public void addReceiving(String ckid, String owner, List<WmsPalletSorting> sortings) throws ServiceException {
        WmsEstimatedArrivalOrder est = dao.selectASNdd(sortings.get(0).getAsnId());
        if ("0".equals(est.getSczt())) {
            //存储收货表中的数据用于循环添加
            List<WmsReceiving> receivingsList = new ArrayList<WmsReceiving>();
            //存储库存表中的数据用于循环添加
            List<WmsStock> stocksList = new ArrayList<WmsStock>();
            //存储码盘表中的数据用于批量修改码盘表
            List<WmsPalletSorting> sortingList = new ArrayList<WmsPalletSorting>();
            //存储预期到货通知单明细 修改状态
            List<WmsEstimatedArrivalOrderDetailed> detailedsList = new ArrayList<WmsEstimatedArrivalOrderDetailed>();
            //用于时间转换
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //用于存储破损数量
            int ppsl = 0;
            //用于存储拒收数量
            int jssl = 0;
            //用于存储收货数量
            int shsl = 0;
            //用于存放预发货单明细Id
            String mxid = "";
            //遍历参数，取出条数
            for (WmsPalletSorting pallet : sortings) {
                //用于存放库存信息
                WmsStock stock = new WmsStock();
                stock.setId(UUIDUtils.uuid());
                stock.setSshzid(owner);
                stock.setGlyqmxid(pallet.getYqdmxid());
                stock.setKwid(pallet.getKwid());
                stock.setHpid(pallet.getGoodsId());
                stock.setMpid(pallet.getId());
                stock.setHppc(pallet.getHppc());
//                if("0".equals(pallet.getXssl())){
//                    if(pallet.getJssl()!=null){
//                        if(Integer.parseInt(pallet.getJssl())<=0){
//                            throw new RuntimeException("现收数量为0,拒收数量必须大于0");
//                        }
//                    }
//                }
                stock.setSl(pallet.getXssl());
                stock.setDxjsl("0");
                stock.setDzysl("0");
                //小新星修改，日期穿的是String
                //Date dete=new Date(Long.valueOf(pallet.getScrq()));
                stock.setScrq(pallet.getScrq());
                //货品有效期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                WmsEstimatedArrivalOrderDetailed des = wmsEstimatedArrivalOrderDetailedMapper.baseSelectByPrimaryKey(pallet.getYqdmxid());
                stock.setSxrq(des.getSxrq());
                stock.setYdsj(format.format(new Date()));
                stock.setCkid(ckid);
                stock.setRksj(sdf.format(new Date()));
                stock.setCjsj(format.format(new Date()));
                //加入库存集合
                stocksList.add(stock);

                //用于存放收货信息
                WmsPalletSorting sortingMx = dao.seachMpxxByMpid(pallet.getId());
                WmsReceiving receiving = new WmsReceiving();
                receiving.setId(UUIDUtils.uuid());
                receiving.setYqdmxid(pallet.getYqdmxid());
                receiving.setKwid(pallet.getKwid());
                receiving.setKcid(stock.getId());
                receiving.setSjzt("0");
                if (Integer.parseInt(pallet.getXssl()) >= 0) {
                    receiving.setSl(pallet.getXssl());
                } else {
                    throw new RuntimeException("收货数量必须为正整数");
                }
                receiving.setCzr(pallet.getCzry());
                receiving.setCzsj(format.format(new Date()));
                receiving.setCkid(ckid);
                if (sortingMx.getKwid() != null && !("".equals(sortingMx.getKwid()))) {
                    receiving.setTjkwid(sortingMx.getKwid());
                } else {
                    receiving.setTjkwid("");
                }
                //加入收货集合
                receivingsList.add(receiving);
                //用于存放码盘信息
                WmsPalletSorting sorting = new WmsPalletSorting();
                sorting.setId(pallet.getId());
                sorting.setSssl(pallet.getXssl());
                sortingList.add(sorting);
                dao.baseUpdateLibrarypositionByKwid(pallet.getKwid());
                //查询明细中破损数量
                WmsEstimatedArrivalOrderDetailed d = wmsEstimatedArrivalOrderDetailedMapper.baseSelectByPrimaryKey(pallet.getYqdmxid());
                //判断确认增加破损数量

                ppsl = (pallet.getPssl() == null) ? 0 : Integer.parseInt(pallet.getPssl()) + Integer.parseInt(d.getPssl());
                jssl = (pallet.getJssl() == null) ? 0 : Integer.parseInt(pallet.getJssl()) + Integer.parseInt(d.getJssl());
                shsl = (pallet.getXssl() == null) ? 0 : Integer.parseInt(pallet.getXssl()) + Integer.parseInt(d.getSdsl());
                WmsEstimatedArrivalOrderDetailed detailed = new WmsEstimatedArrivalOrderDetailed();
                detailed.setId(pallet.getYqdmxid());
                if (jssl >= 0 && ppsl >= 0) {
                    detailed.setJssl(String.valueOf(jssl));
                    detailed.setPssl(String.valueOf(ppsl));
                    detailed.setSdsl(String.valueOf(shsl));
                } else {
                    throw new RuntimeException("破损数量拒收数量必须为正整数");
                }
                String xdwsl = dao.seachXdwsl(d.getHpid());
                detailed.setSdxsl((shsl + Integer.parseInt(d.getSdsl())) / Integer.parseInt(xdwsl) + "箱" + (shsl + Integer.parseInt(d.getSdsl())) % Integer.parseInt(xdwsl) + "件");
                detailed.setJsyy(pallet.getJsyy());
                wmsEstimatedArrivalOrderDetailedMapper.baseUpdateByPrimaryKeySelective(detailed);
                mxid = pallet.getYqdmxid();
                //PO明细ID更新po明细收货数量
                if (d.getDycgmxid() != null && !("0".equals(d.getDycgmxid()))) {
                    WmsPurchaseOrderDetailed p = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(d.getDycgmxid());
                    WmsPurchaseOrderDetailed orderDetailed = new WmsPurchaseOrderDetailed();
                    orderDetailed.setId(d.getDycgmxid());
                    orderDetailed.setShsl(String.valueOf(Integer.parseInt(pallet.getXssl()) + Integer.parseInt(p.getShsl())));
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(orderDetailed);
                    //修改PO明细状态
                    WmsPurchaseOrderDetailed order = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(d.getDycgmxid());
                    WmsPurchaseOrderDetailed o = new WmsPurchaseOrderDetailed();
                    if (Integer.parseInt(order.getSl()) == Integer.parseInt(order.getShsl())) {
                        o.setZt("2");
                    } else if (Integer.parseInt(order.getSl()) > Integer.parseInt(order.getShsl()) && Integer.parseInt(order.getShsl()) != 0) {
                        o.setZt("1");
                    }
                    o.setId(order.getId());
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(o);
                }
            }
            //新增库存信息
            int stocksStatus = wmsStockMapper.baseInsert(stocksList);
            int receivingStatus = 0;
            //新增收货信息
            if (stocksStatus > 0) {
                receivingStatus = dao.baseInsert(receivingsList);
            }
            int sortingStatus = 0;
            //更新码盘实收数量
            if (receivingStatus > 0) {
                sortingStatus = wmsPalletSortingMapper.baseUpdateByPrimaryKeySelective(sortingList);
            }

            //修改状态
            int orderStatus = 0;
            if (sortingStatus > 0) {
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> detaileds = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(sortings.get(0).getAsnId());
                for (WmsEstimatedArrivalOrderDetailed d : detaileds) {
                    WmsEstimatedArrivalOrderDetailed detai = new WmsEstimatedArrivalOrderDetailed();
                    //获取单个明细的收货数量
                    String k = dao.baseSelectCountSl(d.getId());
                    //判断明细收货状态
                    if (k != null) {
                        int f = Integer.parseInt(k);
                        if (f > 0) {
                            if (f == Integer.parseInt(d.getYqsl())) {
                                detai.setZt("2");
                                detai.setId(d.getId());

                            } else if (f < Integer.parseInt(d.getYqsl())) {
                                detai.setZt("1");
                                detai.setId(d.getId());
                            } else {
                                detai.setZt("0");
                                detai.setId(d.getId());
                            }
                        }
                        wmsEstimatedArrivalOrderDetailedMapper.baseUpdateZt(detai);
                    }
                }

            }
            //用于判断预期收货单单据状态
            int bf = 0;//部分收货
            int qb = 0;//全部收货
            int qt = 0;//其他情况
            //查询所有的明细
            List<WmsEstimatedArrivalOrderDetailed> det = wmsEstimatedArrivalOrderDetailedMapper.getBySsyqdid(sortings.get(0).getAsnId());

            for (WmsEstimatedArrivalOrderDetailed d : det) {
                if (d.getZt().equals("1")) {
                    bf = 1;
                } else if (d.getZt().equals("2")) {
                    qb = 1;
                } else {
                    if (d.getZt().equals("0")) {
                        qt = 1;
                    }

                }
            }
            //修改ASN状
            WmsEstimatedArrivalOrder ordere = new WmsEstimatedArrivalOrder();
            ordere.setId(sortings.get(0).getAsnId());
            if (bf == 1 || (qb == 1 && qt == 1)) {
                //部分收货
                ordere.setAsnzt("1");
            } else if (qb == 1 && bf == 0 && qt == 0) {
                //全部收货
                ordere.setAsnzt("2");
            } else {
                throw new RuntimeException("Asn明细状态有异常");
            }
            ordere.setShwcsj(format.format(new Date()));
            orderStatus = wmsEstimatedArrivalOrderMapper.baseUpdateByPrimaryKeySelective(ordere);
            int pStatus = 0;
            if (orderStatus > 0) {
                //查询对应采购订单Id
                WmsEstimatedArrivalOrder arrivalOrder = wmsEstimatedArrivalOrderMapper.baseSelectByPrimaryKey(sortings.get(0).getAsnId());
                if (arrivalOrder.getDycgdid() != null && !("0".equals(arrivalOrder.getDycgdid()))) {
                    //根据采购订单ID查询
                    List<WmsPurchaseOrderDetailed> purckases = wmsPurchaseOrderDetailedMapper.baseSelectBySScgdid(arrivalOrder.getDycgdid());
                    int bfsh = 0;//部分收货
                    int qbsh = 0;//全部收货
                    int qtsh = 0;//其他情况
                    for (WmsPurchaseOrderDetailed o : purckases) {
                        if ("1".equals(o.getZt())) {
                            bfsh = 1;
                        } else if ("2".equals(o.getZt())) {
                            qbsh = 1;
                        } else {
                            if ("0".equals(o.getZt())) {
                                qtsh = 1;
                            }
                        }
                    }
                    //修改采购订单状态
                    WmsPurchaseOrder order = new WmsPurchaseOrder();
                    order.setId(purckases.get(0).getSscgdid());
                    if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                        //部分收货
                        order.setZt("1");
                    } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                        //全部收货
                        order.setZt("2");
                    } else {
                        throw new RuntimeException("确认收货");
                    }
                    order.setShwcsj(format.format(new Date()));
                    pStatus = wmsPurchaseOrderMapper.baseUpdateByPrimaryKeySelective(order);
                }
            } else {
                throw new ServiceException("确认收货");
            }
            if (pStatus < 0) {
                throw new ServiceException("确认收货");
            }
        } else {
            throw new ServiceException("此单据已在其他设备上操作");
        }
    }

    /**
     * 收货确认自动分配储位
     *
     * @param asnId 预期到货通知单ID
     * @return int
     */

    @BaseServiceDescribe(moduleName = "确认收货系统按库位组", btnName = "确认收货系统按库位组指定上架")
    @Transactional
    public void addfkAutomaticReceiving(String ckid, String owner, String userName, String asnId) throws ServiceException {
        WmsEstimatedArrivalOrder est = dao.selectASNdd(asnId);
        //用于时间转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ("0".equals(est.getSczt()) && ("1".equals(est.getAsnzt()) || "2".equals(est.getAsnzt()))) {
            List<WmsReceiving> receivings = dao.selectMxByYqdid(asnId);
            for (WmsReceiving receiving : receivings) {
                //自动分配储位
                int goodsSl = Integer.parseInt(receiving.getSl());
                List<WmsGoodsLocation> locations = wmsGoodsLocationMapper.seachGroupKwidByHpid(receiving.getHpid());//查询改货品所绑定的库位组详细信息
                WmsShelves shelves = new WmsShelves();
                shelves.setCkid(ckid);
                shelves.setSsshid(owner);
                shelves.setCzr(userName);
                shelves.setCzsj(format.format(new Date()));
                //是否有值
                if (locations.size() > 0) {
                    //循环单个库位处理
                    for (WmsGoodsLocation l : locations) {
                        //库位详细信息，托盘数，是否可以混放以及批次是否可以混放
                        WmsLibraryPosition position = dao.selectKwXx(l.getKwid());
                        if (position != null) {
                            //库位上目前是否有货品
                            Integer stocks = dao.seachHpzlByKwid(l.getKwid());
                            WmsPackingDetailed detailed1 = dao.seachTsl(receiving.getHpid());//一托可以装多少货品
                            //是否有货品
                            if (stocks > 0) {
                                int syts = Integer.parseInt(position.getTps());//记录库位剩余托数
                                int yzts = 0;//已经占托数
                                //库存中货品占了多少托
                                List<WmsStock> stockList = dao.sumKwHaveHpsl(l.getKwid());//计算货品占了多少托
                                for (WmsStock s : stockList) {
                                    WmsPackingDetailed pack = dao.seachTsl(s.getHpid());//一托可以装多少货品
                                    System.out.println(pack.getZdwsl());
                                    System.out.println(s.getSl());
                                    if (pack.getZdwsl() != null) {
                                        int ts = Integer.parseInt(s.getSl()) / Integer.parseInt(pack.getZdwsl());//现收货品可放托盘数
                                        int tys = Integer.parseInt(s.getSl()) % Integer.parseInt(pack.getZdwsl());//不足一托的剩余数量
                                        if (tys != 0) {
                                            yzts = ts + 1;
                                        } else {
                                            yzts = ts;
                                        }
                                        syts -= yzts;
                                    }
                                }
                                if (syts > 0) {//有空余托盘数
                                    //有货品
                                    List<WmsStock> st = dao.seachKwHaveHp(receiving.getHpid(), l.getKwid());//有没有同货品
                                    if (st.size() > 0) {
                                        //货品相同
                                        if ("1".equals(position.getPchf())) {
                                            //相同货品批次可以混放
                                            int kcsl = 0;
                                            for (WmsStock t : st) {
                                                kcsl += Integer.parseInt(t.getSl());
                                            }
                                            int qy = kcsl % Integer.parseInt(detailed1.getZdwsl());
                                            if (qy > 0) {
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(Integer.parseInt(detailed1.getZdwsl()) - qy));
                                                goodsSl -= Integer.parseInt(detailed1.getZdwsl()) - qy;//记录未装箱数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                            }
                                        } else {
                                            //不可混放
                                            //是否有同一批次
                                            boolean pc = false;
                                            int kcsl = 0;//记录同一批次的库存数量
                                            for (WmsStock t : st) {
                                                if (t.getHppc().equals(receiving.getHppc())) {
                                                    pc = true;
                                                    kcsl += Integer.parseInt(t.getSl());
                                                }
                                            }
                                            if (pc == true) {
                                                //有同一批次
                                                int qy = kcsl % Integer.parseInt(detailed1.getZdwsl());
                                                if (qy > 0) {
                                                    shelves.setId(UUIDUtils.uuid());
                                                    shelves.setKcid("0");
                                                    shelves.setKwid(l.getKwid());
                                                    shelves.setSjsl(String.valueOf(Integer.parseInt(detailed1.getZdwsl()) - qy));
                                                    goodsSl -= Integer.parseInt(detailed1.getZdwsl()) - qy;//记录未装箱数量
                                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                                }


                                            }

                                        }
                                        //已经占有托盘没有空位放入剩余空托盘中

                                        int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//可放托盘数
                                        int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//剩余不足整托数量
                                        if (ts <= syts && ts != 0) {
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                            goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                            int t = wmsShelvesMapper.baseInsert(shelves);
                                            if (syts > ts && tys > 0) {//库位大于数量
                                                //有剩余托盘可放不满一托货品
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(goodsSl));
                                                goodsSl -= goodsSl;//记录未装箱数量
                                                int k = wmsShelvesMapper.baseInsert(shelves);
                                                if (goodsSl == 0) {
                                                    break;
                                                }
                                            }
                                        } else if (ts > syts) {
                                            //一个库位放不下所有的货品
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(syts * Integer.parseInt(detailed1.getZdwsl())));
                                            goodsSl -= syts * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                            int b = wmsShelvesMapper.baseInsert(shelves);
                                        } else if (ts == 0 && tys > 0) {
                                            //所有的货品不满一托的时候
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(tys));
                                            goodsSl -= tys;//记录剩余未装数量
                                            int e = wmsShelvesMapper.baseInsert(shelves);
                                            if (goodsSl == 0) {
                                                break;
                                            }
                                        }
                                    } else {
                                        //不同货品
                                        if ("1".equals(position.getHphf())) {
                                            //允许混放则放入货品，不允许混放则循环下一个库位
                                            int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//可放托盘数
                                            int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//剩余不足整托数量
                                            if (ts <= syts && ts != 0) {
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                                goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                                if (syts > ts && tys > 0) {//库位大于数量
                                                    //有剩余托盘可放不满一托货品
                                                    shelves.setId(UUIDUtils.uuid());
                                                    shelves.setKcid("0");
                                                    shelves.setKwid(l.getKwid());
                                                    shelves.setSjsl(String.valueOf(goodsSl));
                                                    goodsSl -= goodsSl;//记录未装箱数量
                                                    int t = wmsShelvesMapper.baseInsert(shelves);
                                                    if (goodsSl == 0) {
                                                        break;
                                                    }
                                                }
                                            } else if (ts > syts) {
                                                //一个库位放不下所有的货品
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(syts * Integer.parseInt(detailed1.getZdwsl())));
                                                goodsSl -= syts * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                            } else if (ts == 0 && tys > 0) {
                                                //所有的货品不满一托的时候
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(tys));
                                                goodsSl -= tys;//记录剩余未装数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                                if (goodsSl == 0) {
                                                    break;
                                                }
                                            }

                                        } else {
                                            continue;
                                        }
                                    }
                                }
                            } else {
                                //无货品，取托盘数量直接放货品
                                int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//现收货品可放托盘数
                                int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//不足一托的剩余数量
                                int tps = Integer.parseInt(position.getTps());//库位可放托盘数
                                //判断三种类型
                                if (ts <= tps && ts != 0) {
                                    //一次可以全放下所有的货品
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                    goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                    if (tps > ts && tys > 0) {//库位大于数量
                                        //有剩余托盘可放不满一托货品
                                        shelves.setId(UUIDUtils.uuid());
                                        shelves.setKcid("0");
                                        shelves.setKwid(l.getKwid());
                                        shelves.setSjsl(String.valueOf(goodsSl));
                                        goodsSl -= goodsSl;//记录未装箱数量
                                        int t = wmsShelvesMapper.baseInsert(shelves);
                                        if (goodsSl == 0) {
                                            break;
                                        }
                                    }
                                } else if (ts > tps) {
                                    //一个库位放不下所有的货品
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSjsl(String.valueOf(tps * Integer.parseInt(detailed1.getZdwsl())));
                                    goodsSl -= tps * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                } else if (ts == 0 && tys > 0) {
                                    //所有的货品不满一托的时候
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSjsl(String.valueOf(tys));
                                    goodsSl -= tys;//记录剩余未装数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                    if (goodsSl == 0) {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                } else {
                    //无值，则没有绑定库位  生成上架信息，kwid为0
                    shelves.setId(UUIDUtils.uuid());
                    shelves.setKcid("0");
                    shelves.setKwid("0");
                    shelves.setSjsl(receiving.getSl());
                    int f = wmsShelvesMapper.baseInsert(shelves);
                }
            }
        }
    }

    /**
     * 系统最快规则上架
     *
     * @param asnId 预期到货通知单ID
     * @return int
     */
    @BaseServiceDescribe(moduleName = "确认收货系统最快上架", btnName = "确认收货系统最快上架")
    @Transactional
    public void addZkAutomaticReceiving(String ckid, String owner, String userName, String asnId) throws ServiceException {
        WmsEstimatedArrivalOrder est = dao.selectASNdd(asnId);
        //用于时间转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ("0".equals(est.getSczt()) && ("1".equals(est.getAsnzt()) || "2".equals(est.getAsnzt()))) {
            List<WmsReceiving> receivings = dao.selectMxByYqdid(asnId);
            for (WmsReceiving receiving : receivings) {
                //自动分配储位
                int goodsSl = Integer.parseInt(receiving.getSl());
                List<WmsGoodsLocation> locations = wmsGoodsLocationMapper.seachGroupKwidByHpid(receiving.getHpid());//查询改货品所绑定的库位组详细信息
                WmsShelves shelves = new WmsShelves();
                shelves.setCkid(ckid);
                shelves.setSsshid(owner);
                shelves.setCzr(userName);
                shelves.setCzsj(format.format(new Date()));
                WmsPackingDetailed detailed1 = dao.seachTsl(receiving.getHpid());//一托可以装多少货品
                //是否有值
                if (locations.size() > 0) {
                    //循环单个库位处理
                    for (WmsGoodsLocation l : locations) {
                        //库位详细信息，托盘数，是否可以混放以及批次是否可以混放
                        WmsLibraryPosition position = dao.selectKwXx(l.getKwid());

                        //库位上目前是否有货品
                        Integer stocks = dao.seachHpzlByKwid(l.getKwid());
                        //是否有货品,无货品的时候放入
                        if (stocks <= 0) {
                            //无货品，取托盘数量直接放货品
                            int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//现收货品可放托盘数
                            int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//不足一托的剩余数量
                            int tps = Integer.parseInt(position.getTps());//库位可放托盘数
                            //判断三种类型
                            if (ts <= tps && ts != 0) {
                                //一次可以全放下所有的货品
                                shelves.setId(UUIDUtils.uuid());
                                shelves.setKcid("0");
                                shelves.setKwid(l.getKwid());
                                shelves.setSl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                int f = wmsShelvesMapper.baseInsert(shelves);
                                if (tps > ts && tys > 0) {//库位大于数量
                                    //有剩余托盘可放不满一托货品
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSjsl(String.valueOf(goodsSl));
                                    goodsSl -= goodsSl;//记录未装箱数量
                                    int t = wmsShelvesMapper.baseInsert(shelves);
                                    if (goodsSl == 0) {
                                        break;
                                    }
                                }
                            } else if (ts > tps) {
                                //一个库位放不下所有的货品
                                shelves.setId(UUIDUtils.uuid());
                                shelves.setKcid("0");
                                shelves.setKwid(l.getKwid());
                                shelves.setSjsl(String.valueOf(tps * Integer.parseInt(detailed1.getZdwsl())));
                                goodsSl -= tps * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                int f = wmsShelvesMapper.baseInsert(shelves);
                            } else if (ts == 0 && tys > 0) {
                                //所有的货品不满一托的时候
                                shelves.setId(UUIDUtils.uuid());
                                shelves.setKcid("0");
                                shelves.setKwid(l.getKwid());
                                shelves.setSjsl(String.valueOf(tys));
                                goodsSl -= tys;//记录剩余未装数量
                                int f = wmsShelvesMapper.baseInsert(shelves);
                                if (goodsSl == 0) {
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    //无值，则没有绑定库位  生成上架信息，kwid为0
                    shelves.setId(UUIDUtils.uuid());
                    shelves.setKcid("0");
                    shelves.setKwid("0");
                    shelves.setSjsl(receiving.getSl());
                    int f = wmsShelvesMapper.baseInsert(shelves);
                }
            }
        }
    }

    /**
     * 取消收货
     *
     * @param ids 收货信息参数
     * @return int
     */

    @BaseServiceDescribe(moduleName = "码盘收货", btnName = "取消收货")
    @Transactional
    public StringBuffer rollBackReceiving(List<String> ids, String qxry, String qxyy) throws ServiceException {
        StringBuffer asnbh = new StringBuffer();
        //判断码盘是否已上架
        int num = 0;
        for (String sid : ids) {
            int acount = dao.getSjCount(sid);
            if (acount > 0) {
                num++;
            }
        }
        if (num <= 0) {
            throw new ServiceException("货物已上架，不可取消收货");
        } else {
            for (String ssyqdid : ids) {
                WmsEstimatedArrivalOrder est = dao.selectASNdd(ssyqdid);
                if (est.getZjzt().equals("0")) {
                    //根据单据查询明细
                    //查询所有的明细
                    List<WmsEstimatedArrivalOrderDetailed> detaileds = wmsEstimatedArrivalOrderDetailedMapper.baseSelectQmxBySsyqdid(ssyqdid);
                    for (WmsEstimatedArrivalOrderDetailed det : detaileds) {
                        //删除库存表信息
                        List<String> stockids = dao.baseSelectKcid(det.getId());//查询要删除的库存Id
                        //根据库存Id查询码盘ID修改码盘实收数量
                        List<String> mpids = wmsStockMapper.baseSelectMpidMyPrimaryKeys(stockids);
                        //根据码盘ID批量修改实收数量
                        int mpStatus = wmsPalletSortingMapper.baseUpdateSssl(mpids);
                        if (mpStatus > 0) {
                            //删除库存信息
                            int stockStatus = wmsStockMapper.baseDeleteBathPrimaryKeys(stockids);
                            if (stockStatus > 0) {
                                List<String> kcids = dao.seachKcid(det.getId());//查询要删除得收货信息
//                            int del = dao.baseDeleteSjByKcid(kcids);//删除上架信息
                                //根据明细Id删除收货信息
                                WmsReceiving re = new WmsReceiving();
                                re.setYqdmxid(det.getId());
                                re.setSfyx("0");
                                re.setQxry(qxry);
                                re.setQxyy(qxyy);
                                Date date = new Date();
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                re.setQxsj(sdf.format(date));
                                int receivingStatus = dao.baseUpdateByYqdmxId(re);
                                if (receivingStatus > 0) {
                                    //判断明细状态
                                    WmsEstimatedArrivalOrderDetailed detai = new WmsEstimatedArrivalOrderDetailed();
                                    //获取单个明细的收货数量
                                    String k = dao.baseSelectCountSl(det.getId());
                                    //判断明细收货状态
                                    //没有数据的情况下是初始状态
                                    if (k != null) {
                                        int f = Integer.parseInt(k);
                                        if (f > 0) {
                                            if (f == Integer.parseInt(det.getYqsl())) {
                                                detai.setZt("2");
                                                detai.setId(det.getId());
                                                detai.setSdsl(k);

                                            } else if (f < Integer.parseInt(det.getYqsl())) {
                                                detai.setZt("1");
                                                detai.setId(det.getId());
                                                detai.setSdsl(k);
                                            }
                                        } else {
                                            //收货表中已经没有此条明细的收货信息则为初始状态
                                            if (4 != Integer.parseInt(det.getZt())) {//不计算取消单据
                                                detai.setZt("0");
                                                detai.setId(det.getId());
                                                detai.setSdsl("0");
                                                detai.setJssl("0");
                                                detai.setPssl("0");
                                            }
                                        }
                                    } else {
                                        detai.setZt("0");
                                        detai.setId(det.getId());
                                        detai.setSdsl("0");
                                        detai.setPssl("0");
                                        detai.setJssl("0");
                                    }


                                    //修改明细状态
                                    wmsEstimatedArrivalOrderDetailedMapper.baseUpdateZt(detai);
                                    //修改PO明细状态
                                    if (det.getDycgmxid() != null && !("0".equals(det.getDycgmxid()))) {
                                        WmsPurchaseOrderDetailed order = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(det.getDycgmxid());
                                        WmsPurchaseOrderDetailed o = new WmsPurchaseOrderDetailed();
                                        if (k != null) {
                                            int f = Integer.parseInt(k);
                                            if (f > 0) {
                                                if (f == Integer.parseInt(order.getSl())) {
                                                    o.setZt("2");
                                                    o.setShsl(k);

                                                } else if (f < Integer.parseInt(order.getSl())) {
                                                    o.setZt("1");
                                                    o.setShsl(k);
                                                }
                                            } else {
                                                //收货表中已经没有此条明细的收货信息则为初始状态
                                                if (3 != Integer.parseInt(order.getZt())) {//不计算取消单据
                                                    o.setZt("0");
                                                    o.setShsl("0");
                                                }
                                            }
                                        } else {
                                            o.setZt("0");
                                            o.setShsl("0");
                                        }
                                        o.setId(order.getId());
                                        wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(o);
                                    }
                                }
                            }
                        }


                    }
                    //修改单据状态

                    //用于判断预期收货单单据状态
                    int bfsh = 0;//部分收货
                    int qbsh = 0;//全部收货
                    int qtsh = 0;//其他情况
                    //查询所有的明细
                    List<WmsEstimatedArrivalOrderDetailed> det = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(ssyqdid);

                    for (WmsEstimatedArrivalOrderDetailed d : det) {
                        if (d.getZt().equals("1")) {
                            bfsh = 1;
                        } else if (d.getZt().equals("2")) {
                            qbsh = 1;
                        } else {
                            if (d.getZt().equals("0")) {//取消单据不算
                                qtsh = 1;
                            }
                        }
                    }
                    //修改ASN状态
                    WmsEstimatedArrivalOrder order = new WmsEstimatedArrivalOrder();
                    order.setId(ssyqdid);
                    if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                        //部分收货
                        order.setAsnzt("1");
                    } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                        //全部收货
                        order.setAsnzt("2");
                    } else if (qtsh == 1 && qbsh == 0 && bfsh == 0) {
                        //初始状态
                        order.setAsnzt("0");
                    }
                    order.setShwcsj(null);
                    int orderStatus = wmsEstimatedArrivalOrderMapper.baseUpdateByPrimaryKeySelective(order);

                    if (orderStatus > 0) {
                        //查询对应采购订单Id
                        WmsEstimatedArrivalOrder arrivalOrder = wmsEstimatedArrivalOrderMapper.baseSelectByPrimaryKey(ssyqdid);
                        if (arrivalOrder.getDycgdid() != null && !("0".equals(arrivalOrder.getDycgdid()))) {
                            //根据采购订单ID查询
                            List<WmsPurchaseOrderDetailed> purckases = wmsPurchaseOrderDetailedMapper.baseSelectBySScgdid(arrivalOrder.getDycgdid());
                            int bfsh1 = 0;//部分收货
                            int qbsh1 = 0;//全部收货
                            int qtsh1 = 0;//其他情况
                            for (WmsPurchaseOrderDetailed o : purckases) {
                                if ("1".equals(o.getZt())) {
                                    bfsh1 = 1;
                                } else if ("2".equals(o.getZt())) {
                                    qbsh1 = 1;
                                } else {
                                    if ("0".equals(o.getZt())) {
                                        qtsh1 = 1;
                                    }
                                }
                            }
                            //修改采购订单状态
                            WmsPurchaseOrder order1 = new WmsPurchaseOrder();
                            order1.setId(purckases.get(0).getSscgdid());
                            if (bfsh1 == 1 || (qbsh == 1 && qtsh == 1)) {
                                //部分收货
                                order1.setZt("1");
                            } else if (qbsh1 == 1 && bfsh1 == 0 && qtsh1 == 0) {
                                //全部收货
                                order1.setZt("2");
                            } else if (qtsh1 == 1 && qbsh1 == 0 && bfsh1 == 0) {
                                //初始状态
                                order1.setZt("0");
                            }
                            order1.setShwcsj(null);
                            wmsPurchaseOrderMapper.baseUpdateByPrimaryKeySelective(order1);
                        }
                    }
                } else {
                    asnbh.append(est.getAsnbh());
                }
            }
        }
        return asnbh;
    }

//     /**
//     * 插入一条记录
//     * @param entity 实体对象
//     * @param database 数据库名称
//     * @return int
//     */
//    public Integer baseInsert( WmsReceiving entity, String database ){
//         return dao.baseInsert( entity,database );
//    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsReceiving entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsReceiving>
     */
    public Page<WmsReceiving> baseSelectPage(WmsReceiving entity) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsReceiving>) dao.baseSelectPage(entity);
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsReceiving
     */
    public WmsReceiving baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsReceiving> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsReceiving entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsReceiving entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }

    /**
     * ASN收货
     *
     * @param owner
     * @param detailed
     * @throws ServiceException Jane
     */
    @BaseServiceDescribe(moduleName = "ASN确认收货", btnName = "ASN确认收货按钮")
    @Transactional
    public void addASNReceiving(String ckid, String owner, List<WmsEstimatedArrivalOrderDetailed> detailed, String id) throws ServiceException {
        WmsEstimatedArrivalOrder est = dao.selectASNdd(id);
        if ("0".equals(est.getSczt())) {
            //存储收货表中的数据用于循环添加
            List<WmsReceiving> receivingsList = new ArrayList<WmsReceiving>();
            //存储库存表中的数据用于循环添加
            List<WmsStock> stocksList = new ArrayList<WmsStock>();
            //存储预期到货通知单明细 修改状态
            List<WmsEstimatedArrivalOrderDetailed> detailedsList = new ArrayList<WmsEstimatedArrivalOrderDetailed>();
            //用于时间转换
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //用于存储破损数量
            int ppsl = 0;
            //用于存储拒收数量
            int jssl = 0;
            //用于存放实收数量
            int shsl = 0;
            //用于存放预发货单明细Id
            String mxid = "";
            //遍历参数，取出条数
            for (WmsEstimatedArrivalOrderDetailed d : detailed) {
                //用于存放库存信息
                WmsStock stock = new WmsStock();
                stock.setId(UUIDUtils.uuid());
                stock.setSshzid(owner);
                stock.setGlyqmxid(d.getId());
                stock.setKwid(d.getKwid());
                stock.setHpid(d.getHpid());
                stock.setMpid("0");
                stock.setHppc(d.getHppc());
//               if("0".equals(d.getXssl())){
//                   if(d.getJssl()!=null){
//                       if(Integer.parseInt(d.getJssl())<=0){
//                           throw new RuntimeException("现收数量为0,拒收数量必须大于0");
//                       }
//                   }
//               }
                if (Integer.parseInt(d.getXssl()) >= 0) {
                    stock.setSl(d.getXssl());
                } else {
                    throw new RuntimeException("收货数量必须为正整数");
                }
                stock.setDxjsl("0");
                stock.setDzysl("0");
               /*Date dete=new Date(Long.valueOf(d.getScrq()));
               stock.setScrq(format.format(dete));*/
                stock.setScrq(d.getScrq());
                //货品有效期
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                WmsEstimatedArrivalOrderDetailed estd = wmsEstimatedArrivalOrderDetailedMapper.baseSelectByPrimaryKey(d.getId());
                stock.setSxrq(estd.getSxrq());
                stock.setYdsj(format.format(new Date()));
                stock.setCkid(ckid);
                stock.setRksj(sdf.format(new Date()));
                stock.setCjsj(format.format(new Date()));
                //加入库存集合
                stocksList.add(stock);
                //用于存放收货信息
                WmsReceiving receiving = new WmsReceiving();
                receiving.setId(UUIDUtils.uuid());
                receiving.setYqdmxid(d.getId());
                receiving.setKwid(d.getKwid());
                receiving.setKcid(stock.getId());
                receiving.setSjzt("0");
                receiving.setSl(d.getXssl());
                receiving.setCzr(d.getCzry());
                receiving.setCzsj(format.format(new Date()));
                receiving.setTjkwid("");
                receiving.setCkid(ckid);
                //加入收货集合
                receivingsList.add(receiving);
                dao.baseUpdateLibrarypositionByKwid(d.getKwid());
                //查询明细中破损数量
                WmsEstimatedArrivalOrderDetailed des = wmsEstimatedArrivalOrderDetailedMapper.baseSelectByPrimaryKey(d.getId());
                //判断确认增加破损数量
                ppsl = d.getPssl() == null ? 0 : Integer.parseInt(d.getPssl()) + Integer.parseInt(des.getPssl());
                jssl = d.getJssl() == null ? 0 : Integer.parseInt(d.getJssl()) + Integer.parseInt(des.getJssl());
                shsl = d.getXssl() == null ? 0 : Integer.parseInt(d.getXssl()) + Integer.parseInt(des.getSdsl());

                WmsEstimatedArrivalOrderDetailed detailedsf = new WmsEstimatedArrivalOrderDetailed();
                detailedsf.setId(d.getId());
                if (jssl >= 0 && ppsl >= 0) {
                    detailedsf.setJssl(String.valueOf(jssl));
                    detailedsf.setPssl(String.valueOf(ppsl));
                    detailedsf.setSdsl(String.valueOf(shsl));
                } else {
                    throw new RuntimeException("现收数量或拒收数量必须为正整数");
                }
                String xdwsl = dao.seachXdwsl(d.getHpid());
                detailedsf.setSdxsl((shsl + Integer.parseInt(des.getSdsl())) / Integer.parseInt(xdwsl) + "箱" + (shsl + Integer.parseInt(des.getSdsl())) % Integer.parseInt(xdwsl) + "件");
                detailedsf.setJsyy(d.getJsyy());
                detailedsList.add(detailedsf);
                wmsEstimatedArrivalOrderDetailedMapper.baseUpdateByMxZt(detailedsList);
                mxid = d.getId();
                //PO明细ID更新po明细收货数量
                if (des.getDycgmxid() != null && !("0".equals(des.getDycgmxid()))) {
                    WmsPurchaseOrderDetailed p = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(des.getDycgmxid());
                    WmsPurchaseOrderDetailed orderDetailed = new WmsPurchaseOrderDetailed();
                    orderDetailed.setId(des.getDycgmxid());
                    orderDetailed.setShsl(String.valueOf(Integer.parseInt(d.getXssl()) + Integer.parseInt(p.getShsl())));
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(orderDetailed);
                    //修改PO明细状态
                    WmsPurchaseOrderDetailed order = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(des.getDycgmxid());
                    WmsPurchaseOrderDetailed o = new WmsPurchaseOrderDetailed();
                    if (Integer.parseInt(order.getSl()) == Integer.parseInt(order.getShsl())) {
                        o.setZt("2");
                    } else if (Integer.parseInt(order.getSl()) > Integer.parseInt(order.getShsl()) && Integer.parseInt(order.getShsl()) != 0) {
                        o.setZt("1");
                    }
                    o.setId(order.getId());
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(o);
                }
            }
            //新增库存信息
            int stocksStatus = wmsStockMapper.baseInsert(stocksList);
            int receivingStatus = 0;
            //新增收货信息
            if (stocksStatus > 0) {
                receivingStatus = dao.baseInsert(receivingsList);
            }


            //修改状态
            int orderStatus = 0;
            if (receivingStatus > 0) {
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> detaileds = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(id);
                for (WmsEstimatedArrivalOrderDetailed d : detaileds) {
                    WmsEstimatedArrivalOrderDetailed detai = new WmsEstimatedArrivalOrderDetailed();
                    //获取单个明细的收货数量
                    String k = dao.baseSelectCountSl(d.getId());
                    //判断明细收货状态
                    if (k != null) {
                        int f = Integer.parseInt(k);
                        if (f > 0) {
                            if (f == Integer.parseInt(d.getYqsl())) {
                                detai.setZt("2");
                                detai.setId(d.getId());

                            } else if (f < Integer.parseInt(d.getYqsl())) {
                                detai.setZt("1");
                                detai.setId(d.getId());
                            } else {
                                throw new RuntimeException("确认收货");
                            }
                        } else {
                            throw new RuntimeException("确认收货");
                        }
                        wmsEstimatedArrivalOrderDetailedMapper.baseUpdateZt(detai);
                    }
                }
                //用于判断预期收货单单据状态
                int bfsh = 0;//部分收货
                int qbsh = 0;//全部收货
                int qtsh = 0;//其他情况
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> det = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(id);

                for (WmsEstimatedArrivalOrderDetailed d : det) {
                    if ("1".equals(d.getZt())) {
                        bfsh = 1;
                    } else if ("2".equals(d.getZt())) {
                        qbsh = 1;
                    } else {
                        if ("0".equals(d.getZt())) {
                            qtsh = 1;
                        }

                    }
                }
                //修改ASN状态
                WmsEstimatedArrivalOrder order = new WmsEstimatedArrivalOrder();
                order.setId(id);
                if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                    //部分收货
                    order.setAsnzt("1");
                } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                    //全部收货
                    order.setAsnzt("2");
                } else {
                    throw new RuntimeException("确认收货");
                }
                order.setShwcsj(format.format(new Date()));
                orderStatus = wmsEstimatedArrivalOrderMapper.baseUpdateByPrimaryKeySelective(order);
            }
            int pStatus = 0;
            if (orderStatus > 0) {
                //查询对应采购订单Id
                WmsEstimatedArrivalOrder arrivalOrder = wmsEstimatedArrivalOrderMapper.baseSelectByPrimaryKey(id);
                if (arrivalOrder.getDycgdid() != null && !("0".equals(arrivalOrder.getDycgdid()))) {
                    //根据采购订单ID查询
                    List<WmsPurchaseOrderDetailed> purckases = wmsPurchaseOrderDetailedMapper.baseSelectBySScgdid(arrivalOrder.getDycgdid());
                    int bfsh = 0;//部分收货
                    int qbsh = 0;//全部收货
                    int qtsh = 0;//其他情况
                    for (WmsPurchaseOrderDetailed o : purckases) {
                        if ("1".equals(o.getZt())) {
                            bfsh = 1;
                        } else if ("2".equals(o.getZt())) {
                            qbsh = 1;
                        } else {
                            if ("0".equals(o.getZt())) {
                                qtsh = 1;
                            }
                        }
                    }
                    //修改采购订单状态
                    WmsPurchaseOrder order = new WmsPurchaseOrder();
                    order.setId(purckases.get(0).getSscgdid());
                    if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                        //部分收货
                        order.setZt("1");
                    } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                        //全部收货
                        order.setZt("2");
                    } else {
                        throw new RuntimeException("确认收货");
                    }
                    order.setShwcsj(format.format(new Date()));
                    pStatus = wmsPurchaseOrderMapper.baseUpdateByPrimaryKeySelective(order);
                }
            } else {
                throw new ServiceException("确认收货");
            }
            if (pStatus < 0) {
                throw new ServiceException("确认收货");
            }
        } else {
            throw new ServiceException("此单据已在其他设备上操作");
        }
    }

    /**
     * ASN收货
     *
     * @param owner
     * @param detailed
     * @throws ServiceException Jane
     */
    @BaseServiceDescribe(moduleName = "ASN确认收货", btnName = "ASN确认收货自动分配储位按钮")
    @Transactional
    public void addAutomaticASNReceiving(String ckid, String owner, String userName, List<WmsEstimatedArrivalOrderDetailed> detailed, String id) throws ServiceException {
        WmsEstimatedArrivalOrder est = dao.selectASNdd(id);
        if ("0".equals(est.getSczt())) {
            //存储收货表中的数据用于循环添加
            List<WmsReceiving> receivingsList = new ArrayList<WmsReceiving>();
            //存储库存表中的数据用于循环添加
            List<WmsStock> stocksList = new ArrayList<WmsStock>();
            //存储预期到货通知单明细 修改状态
            List<WmsEstimatedArrivalOrderDetailed> detailedsList = new ArrayList<WmsEstimatedArrivalOrderDetailed>();
            //用于时间转换
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //用于存储破损数量
            int ppsl = 0;
            //用于存储拒收数量
            int jssl = 0;
            //用于存放实收数量
            int shsl = 0;
            //用于存放预发货单明细Id
            String mxid = "";
            //遍历参数，取出条数
            for (WmsEstimatedArrivalOrderDetailed d : detailed) {
                //用于存放库存信息
                WmsStock stock = new WmsStock();
                stock.setId(UUIDUtils.uuid());
                stock.setSshzid(owner);
                stock.setGlyqmxid(d.getId());
                stock.setKwid(d.getKwid());
                stock.setHpid(d.getHpid());
                stock.setMpid("0");
                stock.setHppc(d.getHppc());
                stock.setSl(d.getXssl());
                stock.setDxjsl("0");
                stock.setDzysl("0");
                Date dete = new Date(Long.valueOf(d.getScrq()));
                stock.setScrq(format.format(dete));
                //货品有效期
                String goodsyxq = wmsGoodsMapper.seachGoodsBzqById(d.getHpid());
                Calendar calendar = new GregorianCalendar();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if ("".equals(format.format(dete)) || "0".equals(format.format(dete))) {
                    stock.setSxrq("0000-00-00");
                } else {
                    calendar.setTime(dete);
                    calendar.add(calendar.DATE, Integer.parseInt(goodsyxq));
                    stock.setSxrq(sdf.format(dete));
                }
                stock.setYdsj(format.format(new Date()));
                stock.setCkid(ckid);
                stock.setRksj(sdf.format(new Date()));
                stock.setCjsj(format.format(new Date()));
                //加入库存集合
                stocksList.add(stock);
                //用于存放收货信息
                WmsReceiving receiving = new WmsReceiving();
                receiving.setId(UUIDUtils.uuid());
                receiving.setYqdmxid(d.getId());
                receiving.setKwid(d.getKwid());
                receiving.setKcid(stock.getId());
                receiving.setSjzt("0");
                receiving.setSl(d.getXssl());
                receiving.setCzr(d.getCzry());
                receiving.setCzsj(format.format(new Date()));
                receiving.setCkid(ckid);
                //加入收货集合
                receivingsList.add(receiving);
                //自动分配储位
                int goodsSl = Integer.parseInt(d.getXssl());
                List<WmsGoodsLocation> locations = wmsGoodsLocationMapper.seachGroupKwidByHpid(d.getHpid());//查询改货品所绑定的库位组详细信息
                WmsShelves shelves = new WmsShelves();
                shelves.setCkid(ckid);
                shelves.setSsshid(owner);
                shelves.setCzr(userName);
                shelves.setCzsj(format.format(new Date()));
                //是否有值
                if (locations.size() > 0) {
                    //循环单个库位处理
                    for (WmsGoodsLocation l : locations) {
                        //库位详细信息，托盘数，是否可以混放以及批次是否可以混放
                        WmsLibraryPosition position = dao.selectKwXx(l.getKwid());
                        if (position != null) {
                            //库位上目前是否有货品
                            Integer stocks = dao.seachHpzlByKwid(l.getKwid());
                            WmsPackingDetailed detailed1 = dao.seachTsl(d.getHpid());//一托可以装多少货品
                            //是否有货品
                            if (stocks > 0) {
                                int syts = Integer.parseInt(position.getTps());//记录库位剩余托数
                                int yzts = 0;//已经占托数
                                //库存中货品占了多少托
                                List<WmsStock> stockList = dao.sumKwHaveHpsl(l.getKwid());//计算货品占了多少托
                                for (WmsStock s : stockList) {
                                    WmsPackingDetailed pack = dao.seachTsl(s.getHpid());//一托可以装多少货品
                                    System.out.println(pack.getZdwsl());
                                    System.out.println(s.getSl());
                                    if (pack.getZdwsl() != null) {
                                        int ts = Integer.parseInt(s.getSl()) / Integer.parseInt(pack.getZdwsl());//现收货品可放托盘数
                                        int tys = Integer.parseInt(s.getSl()) % Integer.parseInt(pack.getZdwsl());//不足一托的剩余数量
                                        if (tys != 0) {
                                            yzts = ts + 1;
                                        } else {
                                            yzts = ts;
                                        }
                                        syts -= yzts;
                                    } else {
                                        throw new ServiceException("货品或者绑定包装异常");
                                    }
                                }
                                if (syts > 0) {//有空余托盘数
                                    //有货品
                                    List<WmsStock> st = dao.seachKwHaveHp(d.getHpid(), l.getKwid());//有没有同货品
                                    if (st.size() > 0) {
                                        //货品相同
                                        if ("1".equals(position.getPchf())) {
                                            //相同货品批次可以混放
                                            int kcsl = 0;
                                            for (WmsStock t : st) {
                                                kcsl += Integer.parseInt(t.getSl());
                                            }
                                            int qy = kcsl % Integer.parseInt(detailed1.getZdwsl());
                                            if (qy > 0) {
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(Integer.parseInt(detailed1.getZdwsl()) - qy));
                                                goodsSl -= Integer.parseInt(detailed1.getZdwsl()) - qy;//记录未装箱数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                                if (f <= 0) {
                                                    throw new ServiceException("自动分配储位新增失败");
                                                }
                                            }
                                        } else {
                                            //不可混放
                                            //是否有同一批次
                                            boolean pc = false;
                                            int kcsl = 0;//记录同一批次的库存数量
                                            for (WmsStock t : st) {
                                                if (t.getHppc().equals(d.getHppc())) {
                                                    pc = true;
                                                    kcsl += Integer.parseInt(t.getSl());
                                                }
                                            }
                                            if (pc == true) {
                                                //有同一批次
                                                int qy = kcsl % Integer.parseInt(detailed1.getZdwsl());
                                                if (qy > 0) {
                                                    shelves.setId(UUIDUtils.uuid());
                                                    shelves.setKcid("0");
                                                    shelves.setKwid(l.getKwid());
                                                    shelves.setSjsl(String.valueOf(Integer.parseInt(detailed1.getZdwsl()) - qy));
                                                    goodsSl -= Integer.parseInt(detailed1.getZdwsl()) - qy;//记录未装箱数量
                                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                                    if (f <= 0) {
                                                        throw new ServiceException("自动分配储位新增失败");
                                                    }
                                                }


                                            }

                                        }
                                        //已经占有托盘没有空位放入剩余空托盘中

                                        int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//可放托盘数
                                        int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//剩余不足整托数量
                                        if (ts <= syts && ts != 0) {
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                            goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                            int t = wmsShelvesMapper.baseInsert(shelves);

                                            if (t <= 0) {
                                                throw new ServiceException("自动分配储位新增失败");
                                            }
                                            if (syts > ts && tys > 0) {//库位大于数量
                                                //有剩余托盘可放不满一托货品
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(goodsSl));
                                                goodsSl -= goodsSl;//记录未装箱数量
                                                int k = wmsShelvesMapper.baseInsert(shelves);
                                                if (goodsSl == 0) {
                                                    break;
                                                }
                                                if (k <= 0) {
                                                    throw new ServiceException("自动分配储位新增失败");
                                                }
                                            }
                                        } else if (ts > syts) {
                                            //一个库位放不下所有的货品
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(syts * Integer.parseInt(detailed1.getZdwsl())));
                                            goodsSl -= syts * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                            int b = wmsShelvesMapper.baseInsert(shelves);
                                            if (b <= 0) {
                                                throw new ServiceException("自动分配储位新增失败");
                                            }
                                        } else if (ts == 0 && tys > 0) {
                                            //所有的货品不满一托的时候
                                            shelves.setId(UUIDUtils.uuid());
                                            shelves.setKcid("0");
                                            shelves.setKwid(l.getKwid());
                                            shelves.setSjsl(String.valueOf(tys));
                                            goodsSl -= tys;//记录剩余未装数量
                                            int e = wmsShelvesMapper.baseInsert(shelves);
                                            if (goodsSl == 0) {
                                                break;
                                            }
                                            if (e <= 0) {
                                                throw new ServiceException("自动分配储位新增失败");
                                            }
                                        }
                                    } else {
                                        //不同货品
                                        if ("1".equals(position.getHphf())) {
                                            //允许混放则放入货品，不允许混放则循环下一个库位
                                            int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//可放托盘数
                                            int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//剩余不足整托数量
                                            if (ts <= syts && ts != 0) {
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                                goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);

                                                if (f <= 0) {
                                                    throw new ServiceException("自动分配储位新增失败");
                                                }
                                                if (syts > ts && tys > 0) {//库位大于数量
                                                    //有剩余托盘可放不满一托货品
                                                    shelves.setId(UUIDUtils.uuid());
                                                    shelves.setKcid("0");
                                                    shelves.setKwid(l.getKwid());
                                                    shelves.setSjsl(String.valueOf(goodsSl));
                                                    goodsSl -= goodsSl;//记录未装箱数量
                                                    int t = wmsShelvesMapper.baseInsert(shelves);
                                                    if (goodsSl == 0) {
                                                        break;
                                                    }
                                                    if (t <= 0) {
                                                        throw new ServiceException("自动分配储位新增失败");
                                                    }
                                                }
                                            } else if (ts > syts) {
                                                //一个库位放不下所有的货品
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(syts * Integer.parseInt(detailed1.getZdwsl())));
                                                goodsSl -= syts * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                                if (f <= 0) {
                                                    throw new ServiceException("自动分配储位新增失败");
                                                }
                                            } else if (ts == 0 && tys > 0) {
                                                //所有的货品不满一托的时候
                                                shelves.setId(UUIDUtils.uuid());
                                                shelves.setKcid("0");
                                                shelves.setKwid(l.getKwid());
                                                shelves.setSjsl(String.valueOf(tys));
                                                goodsSl -= tys;//记录剩余未装数量
                                                int f = wmsShelvesMapper.baseInsert(shelves);
                                                if (goodsSl == 0) {
                                                    break;
                                                }
                                                if (f <= 0) {
                                                    throw new ServiceException("自动分配储位新增失败");
                                                }
                                            }

                                        } else {
                                            continue;
                                        }
                                    }
                                }
                            } else {
                                //无货品，取托盘数量直接放货品
                                int ts = goodsSl / Integer.parseInt(detailed1.getZdwsl());//现收货品可放托盘数
                                int tys = goodsSl % Integer.parseInt(detailed1.getZdwsl());//不足一托的剩余数量
                                int tps = Integer.parseInt(position.getTps());//库位可放托盘数
                                //判断三种类型
                                if (ts <= tps && ts != 0) {
                                    //一次可以全放下所有的货品
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSl(String.valueOf(ts * Integer.parseInt(detailed1.getZdwsl())));
                                    goodsSl -= ts * Integer.parseInt(detailed1.getZdwsl());//记录未装箱数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                    if (f <= 0) {
                                        throw new ServiceException("自动分配储位新增失败");
                                    }
                                    if (tps > ts && tys > 0) {//库位大于数量
                                        //有剩余托盘可放不满一托货品
                                        shelves.setId(UUIDUtils.uuid());
                                        shelves.setKcid("0");
                                        shelves.setKwid(l.getKwid());
                                        shelves.setSjsl(String.valueOf(goodsSl));
                                        goodsSl -= goodsSl;//记录未装箱数量
                                        int t = wmsShelvesMapper.baseInsert(shelves);
                                        if (goodsSl == 0) {
                                            break;
                                        }
                                        if (t <= 0) {
                                            throw new ServiceException("自动分配储位新增失败");
                                        }
                                    }
                                } else if (ts > tps) {
                                    //一个库位放不下所有的货品
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSjsl(String.valueOf(tps * Integer.parseInt(detailed1.getZdwsl())));
                                    goodsSl -= tps * Integer.parseInt(detailed1.getZdwsl());//记录剩余未装数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                    if (f <= 0) {
                                        throw new ServiceException("自动分配储位新增失败");
                                    }
                                } else if (ts == 0 && tys > 0) {
                                    //所有的货品不满一托的时候
                                    shelves.setId(UUIDUtils.uuid());
                                    shelves.setKcid("0");
                                    shelves.setKwid(l.getKwid());
                                    shelves.setSjsl(String.valueOf(tys));
                                    goodsSl -= tys;//记录剩余未装数量
                                    int f = wmsShelvesMapper.baseInsert(shelves);
                                    if (goodsSl == 0) {
                                        break;
                                    }
                                    if (f <= 0) {
                                        throw new ServiceException("自动分配储位新增失败");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    //无值，则没有绑定库位  生成上架信息，kwid为0
                    shelves.setId(UUIDUtils.uuid());
                    shelves.setKcid("0");
                    shelves.setKwid("0");
                    shelves.setSjsl(d.getXssl());
                    int f = wmsShelvesMapper.baseInsert(shelves);
                    if (f <= 0) {
                        throw new ServiceException("自动分配储位新增失败");
                    }
                }
                //查询明细中破损数量
                WmsEstimatedArrivalOrderDetailed des = wmsEstimatedArrivalOrderDetailedMapper.baseSelectByPrimaryKey(d.getId());
                //判断确认增加破损数量
                if (mxid.equals(d.getId())) {
                    ppsl += d.getPssl() == null ? 0 : Integer.parseInt(d.getPssl());
                    jssl += d.getJssl() == null ? 0 : Integer.parseInt(d.getJssl());
                    shsl += Integer.parseInt(d.getXssl());
                } else {
                    ppsl = Integer.parseInt(des.getPssl()) + (d.getPssl() == null ? 0 : Integer.parseInt(d.getPssl()));
                    jssl = Integer.parseInt(des.getJssl()) + (d.getJssl() == null ? 0 : Integer.parseInt(d.getJssl()));
                    shsl = Integer.parseInt(des.getSdsl()) + Integer.parseInt(d.getXssl());
                }
                WmsEstimatedArrivalOrderDetailed detailedsf = new WmsEstimatedArrivalOrderDetailed();
                detailedsf.setId(d.getId());
                detailedsf.setJssl(String.valueOf(jssl));
                detailedsf.setPssl(String.valueOf(ppsl));
                detailedsf.setSdsl(String.valueOf(shsl));
                String xdwsl = dao.seachXdwsl(d.getHpid());
                detailedsf.setSdxsl(shsl / Integer.parseInt(xdwsl) + "箱" + shsl % Integer.parseInt(xdwsl) + "件");
                detailedsf.setJsyy(d.getJsyy());
                detailedsList.add(detailedsf);
                mxid = d.getId();
                //PO明细ID更新po明细收货数量
                if (des.getDycgmxid() != null && !("0".equals(des.getDycgmxid()))) {
                    WmsPurchaseOrderDetailed p = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(des.getDycgmxid());
                    WmsPurchaseOrderDetailed orderDetailed = new WmsPurchaseOrderDetailed();
                    orderDetailed.setId(des.getDycgmxid());
                    orderDetailed.setShsl(String.valueOf(Integer.parseInt(d.getXssl()) + Integer.parseInt(p.getShsl())));
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(orderDetailed);
                    //修改PO明细状态
                    WmsPurchaseOrderDetailed order = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(des.getDycgmxid());
                    WmsPurchaseOrderDetailed o = new WmsPurchaseOrderDetailed();
                    if (Integer.parseInt(order.getSl()) == Integer.parseInt(order.getShsl())) {
                        o.setZt("2");
                    } else if (Integer.parseInt(order.getSl()) > Integer.parseInt(order.getShsl()) && Integer.parseInt(order.getShsl()) != 0) {
                        o.setZt("1");
                    }
                    o.setId(order.getId());
                    wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(o);
                }
            }
            //新增库存信息
            int stocksStatus = wmsStockMapper.baseInsert(stocksList);
            int receivingStatus = 0;
            //新增收货信息
            if (stocksStatus > 0) {
                receivingStatus = dao.baseInsert(receivingsList);
            }
            int detailedStatus = 0;
            //更新预收货单明细
            if (receivingStatus > 0) {
                detailedStatus = wmsEstimatedArrivalOrderDetailedMapper.baseUpdateByMxZt(detailedsList);
            }
            //修改状态
            int orderStatus = 0;
            if (detailedStatus > 0) {
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> detaileds = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(id);
                for (WmsEstimatedArrivalOrderDetailed d : detaileds) {
                    WmsEstimatedArrivalOrderDetailed detai = new WmsEstimatedArrivalOrderDetailed();
                    //获取单个明细的收货数量
                    String k = dao.baseSelectCountSl(d.getId());
                    //判断明细收货状态
                    if (k != null) {
                        int f = Integer.parseInt(k);
                        if (f > 0) {
                            if (f == Integer.parseInt(d.getYqsl())) {
                                detai.setZt("2");
                                detai.setId(d.getId());

                            } else if (f < Integer.parseInt(d.getYqsl())) {
                                detai.setZt("1");
                                detai.setId(d.getId());
                            } else {
                                throw new ServiceException("确认收货");
                            }
                        } else {
                            throw new ServiceException("确认收货");
                        }
                        wmsEstimatedArrivalOrderDetailedMapper.baseUpdateZt(detai);
                    }
                }
                //用于判断预期收货单单据状态
                int bfsh = 0;//部分收货
                int qbsh = 0;//全部收货
                int qtsh = 0;//其他情况
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> det = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(id);

                for (WmsEstimatedArrivalOrderDetailed d : det) {
                    if ("1".equals(d.getZt())) {
                        bfsh = 1;
                    } else if ("2".equals(d.getZt())) {
                        qbsh = 1;
                    } else {
                        if ("0".equals(d.getZt())) {
                            qtsh = 1;
                        }

                    }
                }
                //修改ASN状态
                WmsEstimatedArrivalOrder order = new WmsEstimatedArrivalOrder();
                order.setId(id);
                if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                    //部分收货
                    order.setAsnzt("1");
                } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                    //全部收货
                    order.setAsnzt("2");
                } else {
                    throw new ServiceException("确认收货");
                }
                order.setShwcsj(format.format(new Date()));
                orderStatus = wmsEstimatedArrivalOrderMapper.baseUpdateByPrimaryKeySelective(order);
            }
            int pStatus = 0;
            if (orderStatus > 0) {
                //查询对应采购订单Id
                WmsEstimatedArrivalOrder arrivalOrder = wmsEstimatedArrivalOrderMapper.baseSelectByPrimaryKey(id);
                if (arrivalOrder.getDycgdid() != null && !("0".equals(arrivalOrder.getDycgdid()))) {
                    //根据采购订单ID查询
                    List<WmsPurchaseOrderDetailed> purckases = wmsPurchaseOrderDetailedMapper.baseSelectBySScgdid(arrivalOrder.getDycgdid());
                    int bfsh = 0;//部分收货
                    int qbsh = 0;//全部收货
                    int qtsh = 0;//其他情况
                    for (WmsPurchaseOrderDetailed o : purckases) {
                        if ("1".equals(o.getZt())) {
                            bfsh = 1;
                        } else if ("2".equals(o.getZt())) {
                            qbsh = 1;
                        } else {
                            if ("0".equals(o.getZt())) {
                                qtsh = 1;
                            }
                        }
                    }
                    //修改采购订单状态
                    WmsPurchaseOrder order = new WmsPurchaseOrder();
                    order.setId(purckases.get(0).getSscgdid());
                    if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                        //部分收货
                        order.setZt("1");
                    } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                        //全部收货
                        order.setZt("2");
                    } else {
                        throw new ServiceException("确认收货");
                    }
                    order.setShwcsj(format.format(new Date()));
                    pStatus = wmsPurchaseOrderMapper.baseUpdateByPrimaryKeySelective(order);
                }
            } else {
                throw new ServiceException("确认收货");
            }
            if (pStatus < 0) {
                throw new ServiceException("确认收货");
            }
        } else {
            throw new ServiceException("此单据已在其他设备上操作");
        }
    }

    /**
     * 取消收货
     *
     * @param ids 收货信息参数
     * @return int
     */

    @BaseServiceDescribe(moduleName = "ASN收货", btnName = "ASN取消收货")
    @Transactional
    public StringBuffer rollBackASNReceiving(List<String> ids, String qxry, String qxyy) throws ServiceException {
        StringBuffer ansbh = new StringBuffer();
        for (String ssyqdid : ids) {
            WmsEstimatedArrivalOrder est = dao.selectASNdd(ssyqdid);
            if (est.getZjzt().equals("0")) {
                //根据单据查询明细
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> detaileds = wmsEstimatedArrivalOrderDetailedMapper.baseSelectQmxBySsyqdid(ssyqdid);
                for (WmsEstimatedArrivalOrderDetailed det : detaileds) {
                    //删除库存表信息
                    List<String> stockIds = dao.baseSelectKcid(det.getId());//查询要删除的库存Id
                    //删除库存信息
                    int stockStatus = wmsStockMapper.baseDeleteBathPrimaryKeys(stockIds);
                    if (stockStatus > 0) {
                        //根据明细Id删除收货信息
                        List<String> kcids = dao.seachKcid(det.getId());//查询要删除得收货信息
//                        if(kcids.size()>0){
//                            int del = dao.baseDeleteSjByKcid(kcids);//删除上架信息
//                             }
                        WmsReceiving re = new WmsReceiving();
                        re.setYqdmxid(det.getId());
                        re.setSfyx("0");
                        re.setQxry(qxry);
                        re.setQxyy(qxyy);
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        re.setQxsj(sdf.format(date));
                        int receivingStatus = dao.baseUpdateByYqdmxId(re);
                        if (receivingStatus > 0) {
                            //判断明细状态
                            WmsEstimatedArrivalOrderDetailed detai = new WmsEstimatedArrivalOrderDetailed();
                            //获取单个明细的收货数量
                            String k = dao.baseSelectCountSl(det.getId());
                            //判断明细收货状态
                            //没有数据的情况下是初始状态
                            if (k != null) {
                                int f = Integer.parseInt(k);
                                if (f > 0) {
                                    if (f == Integer.parseInt(det.getYqsl())) {
                                        detai.setZt("2");
                                        detai.setId(det.getId());
                                        detai.setSdsl(k);
                                    } else if (f < Integer.parseInt(det.getYqsl())) {
                                        detai.setZt("1");
                                        detai.setId(det.getId());
                                        detai.setSdsl(k);
                                    }
                                } else {
                                    //收货表中已经没有此条明细的收货信息则为初始状态
                                    if (4 != Integer.parseInt(det.getZt())) {//不计算取消单据
                                        detai.setZt("0");
                                        detai.setId(det.getId());
                                        detai.setSdsl("0");
                                        detai.setPssl("0");
                                        detai.setJssl("0");
                                    }
                                }
                            } else {
                                detai.setZt("0");
                                detai.setId(det.getId());
                                detai.setSdsl("0");
                                detai.setPssl("0");
                                detai.setJssl("0");
                            }


                            //修改明细状态
                            wmsEstimatedArrivalOrderDetailedMapper.baseUpdateZt(detai);
                            //修改PO明细状态
                            if (det.getDycgmxid() != null && !("0".equals(det.getDycgmxid()))) {
                                WmsPurchaseOrderDetailed order = wmsPurchaseOrderDetailedMapper.baseSelectByPrimaryKey(det.getDycgmxid());
                                WmsPurchaseOrderDetailed o = new WmsPurchaseOrderDetailed();
                                if (k != null) {
                                    int f = Integer.parseInt(k);
                                    if (f > 0) {
                                        if (f == Integer.parseInt(order.getSl())) {
                                            o.setZt("2");
                                            o.setShsl(k);

                                        } else if (f < Integer.parseInt(order.getSl())) {
                                            o.setZt("1");
                                            o.setShsl(k);
                                        }
                                    } else {
                                        //收货表中已经没有此条明细的收货信息则为初始状态
                                        if (3 != Integer.parseInt(order.getZt())) {//不计算取消单据
                                            o.setZt("0");
                                            o.setShsl("0");
                                        }
                                    }
                                } else {
                                    o.setZt("0");
                                    o.setShsl("0");
                                }
                                o.setId(order.getId());
                                wmsPurchaseOrderDetailedMapper.baseUpdateByPrimaryKeySelective(o);
                            }
                        }
                    }
                }


                //修改单据状态

                //用于判断预期收货单单据状态
                int bfsh = 0;//部分收货
                int qbsh = 0;//全部收货
                int qtsh = 0;//其他情况
                //查询所有的明细
                List<WmsEstimatedArrivalOrderDetailed> det = wmsEstimatedArrivalOrderDetailedMapper.baseSelectBySsyqdid(ssyqdid);

                for (WmsEstimatedArrivalOrderDetailed d : det) {
                    if (d.getZt().equals("1")) {
                        bfsh = 1;
                    } else if (d.getZt().equals("2")) {
                        qbsh = 1;
                    } else {
                        if (d.getZt().equals("0")) {//取消单据不算
                            qtsh = 1;
                        }
                    }
                }
                //修改ASN状态
                WmsEstimatedArrivalOrder order = new WmsEstimatedArrivalOrder();
                order.setId(ssyqdid);
                if (bfsh == 1 || (qbsh == 1 && qtsh == 1)) {
                    //部分收货
                    order.setAsnzt("1");
                } else if (qbsh == 1 && bfsh == 0 && qtsh == 0) {
                    //全部收货
                    order.setAsnzt("2");
                } else if (qtsh == 1 && qbsh == 0 && bfsh == 0) {
                    //初始状态
                    order.setAsnzt("0");
                }
                order.setShwcsj(null);
                int orderStatus = wmsEstimatedArrivalOrderMapper.baseUpdateByPrimaryKeySelective(order);
                if (orderStatus > 0) {
                    //查询对应采购订单Id
                    WmsEstimatedArrivalOrder arrivalOrder = wmsEstimatedArrivalOrderMapper.baseSelectByPrimaryKey(ssyqdid);
                    if (arrivalOrder.getDycgdid() != null && !("0".equals(arrivalOrder.getDycgdid()))) {
                        //根据采购订单ID查询
                        List<WmsPurchaseOrderDetailed> purckases = wmsPurchaseOrderDetailedMapper.baseSelectBySScgdid(arrivalOrder.getDycgdid());
                        int bfsh1 = 0;//部分收货
                        int qbsh1 = 0;//全部收货
                        int qtsh1 = 0;//其他情况
                        for (WmsPurchaseOrderDetailed o : purckases) {
                            if ("1".equals(o.getZt())) {
                                bfsh1 = 1;
                            } else if ("2".equals(o.getZt())) {
                                qbsh1 = 1;
                            } else {
                                if ("0".equals(o.getZt())) {
                                    qtsh1 = 1;
                                }
                            }
                        }
                        //修改采购订单状态
                        WmsPurchaseOrder order1 = new WmsPurchaseOrder();
                        order1.setId(purckases.get(0).getSscgdid());
                        if (bfsh1 == 1) {
                            //部分收货
                            order1.setZt("1");
                        } else if (qbsh1 == 1 && bfsh1 == 0 && qtsh1 == 0) {
                            //全部收货
                            order1.setZt("2");
                        } else if (qtsh1 == 1 && qbsh1 == 0 && bfsh1 == 0) {
                            //初始状态
                            order1.setZt("0");
                        }
                        order1.setShwcsj(null);
                        wmsPurchaseOrderMapper.baseUpdateByPrimaryKeySelective(order1);
                    }
                }
            } else {
                ansbh.append(est.getAsnbh());
            }
        }
        return ansbh;
    }

    /**
     * 查询上架规则_Jane
     *
     * @param owner 货主ID
     * @return int
     */
    @BaseServiceDescribe(moduleName = "确认收货", btnName = "判断质检规则")
    public WmsShelfRule seachSjgz(String owner) {
        return dao.seachSjgz(owner);
    }
}
